//
//  ZBBContentListenNewsRecomendCell.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "ZBBContentListenNewsRecomendCell.h"
#import "TwoToOne.h"
#import "ZBBRecomendCell.h"
#import "ZBBHeadView.h"
#import "HeadView.h"
#import "ZBBScrollViewModel.h"
#import "ZBBScroViewCell.h"
#import "UIImageView+WebCache.h"
#import "FitHeader.h"
#import "ChangeString.h"
#import "S1mpl3AlbumViewController.h"
@implementation ZBBContentListenNewsRecomendCell

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor grayColor];
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:flowLayout];

        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_collectionView];
        [_collectionView registerClass:[ZBBRecomendCell class] forCellWithReuseIdentifier:@"identifiter"];
        
        [_collectionView registerClass:[ZBBScroViewCell class] forCellWithReuseIdentifier:@"identifiter1"];
        [_collectionView registerClass:[ZBBHeadView class]  forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
        [_collectionView registerClass:[HeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head1"];
        
       
    }
    return self;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _dataSourceArray.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
     else
     {
         
         
         return [_dataSourceArray[section] listArray].count;
     }
    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        
        return CGSizeMake(self.frame.size.width, 70 * FITHeight);
    }
    
    else
    {
        return CGSizeMake(self.frame.size.width, 100 * FITHeight);

    }
    
    
    
    
    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    
    
    if (section == 0) {
        
        return CGSizeMake(self.frame.size.width, 200 * FITHeight);
    }
    
    else
    {
        return CGSizeMake(self.frame.size.width, 35 * FITHeight );
    }
    
    
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        ZBBHeadView *headView = [collectionView
        dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
        
        headView.nav = _nav;
        [headView.scrollview shouldAutoShow:YES];
        //headView.scrollview.delegate = self;
        [headView.scrollview setImageViewsWithArray:_scroviewArray];
       
        return headView;
        
    }
    else
    {
   HeadView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head1" forIndexPath:indexPath];
        if (_dataSourceArray.count != 0) {
            ZBBListenRecomendModel *model = _dataSourceArray[indexPath.section];
            headView.leftLabel.text = model.title;
            headView.leftLabel.textColor = [UIColor colorWithRed:0.27 green:0.29 blue:0.3 alpha:1];
            headView.leftLabel.font = [UIFont systemFontOfSize:15];
        }
        
        
        
        return headView;

    }
    
    
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
 
    if (indexPath.section == 0) {
         ZBBScroViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifiter1" forIndexPath:indexPath];
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[_newestDic objectForKey:@"coverPath"]]];
        cell.topLabel.text = [_newestDic objectForKey:@"title"];
        cell.downLabel.text = [_newestDic objectForKey:@"subtitle"];
        return cell;
    }
    else
    {
        ZBBRecomendCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifiter" forIndexPath:indexPath];
   
        if (_dataSourceArray.count != 0) {
    
        cell.model = [_dataSourceArray[indexPath.section] listArray][indexPath.row];
            
        }
     return cell;
    }
}
-(void)setUrlString:(NSString *)urlString
{
           _urlString = urlString;
           _count++;
           if (_count == 2) {
        _scroviewArray = [NSMutableArray array];
        _dataSourceArray = [NSMutableArray arrayWithObject:@""];
        NSString *url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=%@&contentType=album&device=android&scale=2&version=4.3.14.3", urlString];
        [TwoToOne getDataByString:url BodyString:nil WithDataBlock:^(id data) {
            
            NSDictionary *scroViewDic = [data objectForKey:@"focusImages"];
            NSArray *scroViewArray = [scroViewDic objectForKey:@"list"];
            for (NSDictionary *dic in scroViewArray) {
                
                ZBBScrollViewModel *model = [[ZBBScrollViewModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [_scroviewArray addObject:model];
            }
            NSDictionary *categoryContents = [data objectForKey:@"categoryContents"];
            NSArray *contentList = [categoryContents objectForKey:@"list"];
              _newestDic = [contentList[0] objectForKey:@"list"][0];
            for (NSInteger i = 1; i < contentList.count; i++) {
                NSDictionary *dicContent = contentList[i];
                ZBBListenRecomendModel *model = [[ZBBListenRecomendModel alloc]init];
                [model setValuesForKeysWithDictionary:dicContent];
                NSArray *listArray = [dicContent objectForKey:@"list"];
              
                NSMutableArray *array = [NSMutableArray array];
                for (NSDictionary *listDic in listArray) {
                    S1mpl3MoreRecommend *model1 = [[S1mpl3MoreRecommend alloc]init];
                    [model1 setValuesForKeysWithDictionary:listDic];
                    [array addObject:model1];
                   
                }
                 model.listArray = array;
                [_dataSourceArray addObject:model];
            }
            [_collectionView reloadData];
        }];
        
        

    }
   
    
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
  [self.delegate passUrlString:_urlString And:YES];
        
        
        
    }
     else
     {
         
         S1mpl3AlbumViewController *a1vc = [[S1mpl3AlbumViewController alloc]init];
         [self.nav pushViewController:a1vc animated:YES];
         
         a1vc.albumId = [[_dataSourceArray[indexPath.section] listArray][indexPath.row] albumId];
     }
    
    
    

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
